﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using BitAuto.EP.ErrorAlert.DataClient;
using BitAuto.EP.Plat.Util;

namespace BitAuto.EP.CRMLogSystem.Utils
{
    /// <summary>
    /// 预警机制发送邮件
    /// </summary>
    public class ErrorAlertMailHelper
    {
        /// <summary>
        /// 是否发送邮件
        /// </summary>
        private static bool SendEmail
        {
            get { return bool.Parse(AppConfig.GetAppSetting("SendErrorToAdmin")); }
        }

        /// <summary>
        /// 项目ID（由错误监控OP设置）
        /// </summary>
        private static int AppId
        {
            get
            {
                return int.Parse(AppConfig.GetAppSetting("AppId"));
            }
        }
        /// <summary>
        /// 项目ID（由错误监控OP设置）
        /// </summary>
        private static int PC_AppId
        {
            get
            {
                return int.Parse(AppConfig.GetAppSetting("PC_AppId"));
            }
        }
        /// <summary>
        /// 项目ID（由错误监控OP设置）
        /// </summary>
        private static int MobileAppId
        {
            get
            {
                return int.Parse(AppConfig.GetAppSetting("MobileAppId"));
            }
        }


        /// <summary>
        /// 接错误信息的地址
        /// </summary>
        private static string ErrorUrl
        {
            get { return AppConfig.GetAppSetting("ErrorUrl"); }
        }

        /// <summary>
        /// 当前主机IP
        /// </summary>
        private static string CurrentServerIP
        {
            get { return AppConfig.GetAppSetting("CurrentServerIP"); }
        }


        /// <summary>
        /// 通过错误报警系统发送报警邮件（需要有错误报警系统的相关配置）
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="msg">内容</param>
        public static void SendAlertMail(string title, string msg)
        {
            if (SendEmail)
            {
                try
                {
                    // 在出现未处理的错误时运行的代码
                    ClientErrorMsg error = new ClientErrorMsg();
                    error.CreateTime = System.DateTime.Now;
                    error.userIp = CurrentServerIP;
                    error.exceptionMsg = title;
                    error.url = string.Empty;
                    error.exceptionDetail = string.Empty;
                    error.exceptionType = typeof(string).ToString();
                    error.serverIp = CurrentServerIP;
                    error.appId = AppId;
                    error.exceptionLevel = ErrLevel.Error;
                    error.errData = "################" + msg;
                    OperationErrLog.GetInstance(error.appId, ErrorUrl).SaveDataToInterface(error);
                }
                catch (Exception exception)
                {
                    DataLogHelper.E("ErrorAlertMailHelper发送邮件异常:", exception);
                }
            }
        }




    }
}
